<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles">
              <!-- type:index会自动生成序号 -->
              <el-table-column
                label="序号"
                type="index"
                width="50"
                :index="(index)=>{return (index+1) + (pageParams.page-1)*pageParams.pageSize }"
              />
              <el-table-column prop="name" label="角色名称" width="180" />
              <el-table-column prop="description" label="描述" />
              <el-table-column width="300" label="操作">
                <template slot-scope="scope">
                  <el-button size="mini" type="success" @click="hAssign(scope.row)">分配权限</el-button>
                  <el-button size="mini" @click="hEdit(scope.row)">编辑</el-button>
                  <el-button size="mini" type="danger" @click="hDel(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 放置分页组件 -->
            <el-pagination
              background
              layout="prev, pager, next"
              :total="total"
              :current-page.sync="curPage"
              :page-size="pageParams.pageSize"
              @current-change="hCurrentChange"
            />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="30%"
      :title="isEdit ? '编辑':'添加'"
      :visible.sync="showDialog"
      @close="hClose"
    >
      <el-form ref="form" label-width="80px" :model="form" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item>
          <el-button @click="showDialog=false">取消</el-button>
          <el-button type="primary" @click="hSubmit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 分配权限的弹层 -->
    <el-dialog
      v-if="showDialogAssign"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="分配权限"
      :visible.sync="showDialogAssign"
    >
      <AssignPermission
        :cur-id="curId"
        @close="showDialogAssign=false"
      />
    </el-dialog>
  </div>

</template>

<script>
import AssignPermission from './assignPermission.vue'
import { getRoles, deleteRole, addRole, editRole } from '../../api/setting'
export default {
  components: {
    AssignPermission
  },
  data() {
    return {
      activeName: 'first',
      roles: [], // 保存当前数据
      total: 0,
      showDialog: false, // 弹层是否显示
      isEdit: false, // 当前是否是编辑
      curPage: 1,
      showDialogAssign: false,
      curId: '', // 当前是编辑id
      pageParams: {
        page: 1, // 查询第一页
        pageSize: 3// 每页几条
      },
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '起一个角色名称吧', trigger: 'blur' },
          { min: '1', max: '12', message: '名称为1-12位', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '描述一下角色吧', trigger: 'blur' },
          { min: '1', max: '12', message: '稍微描述一下吧', trigger: 'blur' }
        ]

      }
    }
  },
  computed: {
    maxNum() {
      return Math.ceil(this.total / this.pageParams.pageSize)
    },
    // 最后一页是不是满的
    isLastPageFulled: function() {
      return this.total % this.pageParams.pageSize === 0
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 点击页码，自动传入当前页码
    hCurrentChange(curPage) {
      this.pageParams.page = curPage// 更新页码
      this.loadRoles()
    },
    // 获取角色列表
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams.page, this.pageParams.pageSize)
        console.log('获取角色列表', res)
        this.roles = res.data.rows
        this.total = res.data.total

        this.curPage = this.pageParams.page
      } catch (err) {
        console.log(err)
      }
    },
    // 删除
    hDel(id) {
      this.$confirm('确认是否删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        this.doDel(id)
      }).catch(() => {

      })
    },
    async doDel(id) {
      try {
        const res = await deleteRole(id)
        console.log(res)
        if (this.roles.length === 1) {
          this.pageParams.page--
          if (this.pageParams.page === 0) {
            this.pageParams.page = 1
          }
        }
        this.loadRoles()
        this.$message.success('删除成功')
      } catch (err) {
        console.log(err)
        this.loadRoles()
        this.$message.error('删除失败')
      }
    },
    // 添加
    hAdd() {
      this.showDialog = true
    },
    async doAdd() {
      try {
        const res = await addRole(this.form)
        this.showDialog = false
        if (this.isLastPageFulled) {
          this.pageParams.page = this.maxNum + 1
        } else {
          this.pageParams.page = this.maxNum
        }
        this.loadRoles()
        console.log(res)
      } catch (err) {
        console.log(err)
      }
    },
    hSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    // 编辑
    hEdit(row) {
      // 给表单赋值
      this.form = row
      this.showDialog = true

      this.form = { ...row }
      console.log('11111111111111111', this.form)
      // 更新编辑状态
      this.isEdit = true
    },
    async  doEdit() {
      try {
        const res = await editRole(this.form)
        this.showDialog = false

        this.loadRoles()
        console.log(res)
      } catch (err) {
        console.log(err)
      }
    },
    hClose() {
      this.form = {
        name: '',
        description: ''
      }
      this.$refs.form.resetFields()
    },
    hAssign(row) {
      this.curId = row.id
      this.showDialogAssign = true
    }
  }
}
</script>
